<?php

/**
 * User actions.
 *
 * @package    utility
 * @subpackage User
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class UserActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
    $this->forward('User', 'list');
  }
  
public function executeList()
	{
	  $c = new Criteria();
	  $c->add(UserPeer::STATUS, Constant::RECORD_STATUS_DELETED, Criteria::NOT_EQUAL);
	  $c->addAscendingOrderByColumn(UserPeer::NAME);
	  $this->users = UserPeer::doSelect ($c);
  } // - END - executeList 

public function executeAdd(sfWebRequest $request)
{
  	if ($request->isMethod('Post'))
  	{
		$username = $this->getRequestParameter('username');
		$password = $this->getRequestParameter('password');
		
		$c = new Criteria ( );
		$c->add(UserPeer::STATUS, Constant::RECORD_STATUS_DELETED, Criteria::NOT_EQUAL);
		$c->add(UserPeer::USERNAME, $username);
		$check_username = UserPeer::doSelectOne($c);
		
		if ($check_username)
			{
			$this->getUser ()->setFlash ( 'ERROR_MESSAGE', 'Username Already exist. Please Choose Different Username.' );
			$this->redirect ('User/list');
			}
		
		if (strlen($password) != 0)
			{
			$password = md5($password);
			}
		else $password = '';
		
		$user = new User();
		$user->setName($request->getParameter('name'));
		$user->setUsername($username);
		$user->setPassword($password);
		$user->setStatus(Constant::RECORD_STATUS_ACTIVE);
		
		$user->save();
		$this->getUser()->setFlash('SUCCESS_MESSAGE', Constant::RECORD_ADDED_SUCCESSFULLY);
		$this->redirect ('User/list');
	} //end if
 
  } // - END - executeAdd

public function executeEdit (sfWebRequest $request)
	{
		if ($this->getRequest()->getMethod() == sfRequest::POST)
		{
			if (strlen($password) != 0)
				{
				$password = md5($password);
				}
			else $password = '';

			$user = UserPeer::retrieveByPk($this->getRequestParameter('id'));
			$user->setName($request->getParameter('name'));
			$user->setPassword($password);
			
			//Save object to database
			if($user->save())
			{
				$this->getUser()->setFlash('SUCCESS_MESSAGE', Constant::RECORD_EDITED_SUCCESSFULLY);
				$this->redirect ('User/list');
			}
			else
			{
				$this->getUser()->setFlash('ERROR_MESSAGE', Constant::DB_ERROR);
				$this->redirect('User/edit?id='.Utility::EncryptQueryString($request->getParameter('id')));
			}
		}

		else
		{
			$this->user = UserPeer::retrieveByPk(Utility::DecryptQueryString($request->getParameter('id')));
			
		}
	} //- END - executeEdit

public function executeDelete (sfWebRequest $request)
	{
		$station = UserPeer::retrieveByPk(Utility::DecryptQueryString($request->getParameter('id')));

		if($station)
		{
			$station->setStatus(Constant::RECORD_STATUS_DELETED);
			if(	$station->save())
			$this->getUser()->setFlash('SUCCESS_MESSAGE', Constant::RECORD_STATUS_DELETED_SUCCESSFULLY);
			else
			$this->getUser()->setFlash('ERROR_MESSAGE', Constant::DB_ERROR);
		}
		else
		{
			$this->getUser()->setFlash('ERROR_MESSAGE', Constant::INVALID_REQUEST);
		}
	
		$this->redirect ('User/list');

	} //- END - executeDelete
}
